Session 2 - Solving Equations and Inequalities

Rules and Replacement

A rule is a directive to transform expressions into other expression. Rules are denoted by lhs -> rhs meaning that lhs is transformed into rhs. Rules are applied by the functions Replace or ReplaceAll

(* A rule specifying to replace x by 42 *)
x->42
(* The function ReplaceAll applies a rule to some expression *)
ReplaceAll[x^2+x+2,x->2]
(* The shortform for ReplaceAll is expression /. rule *)
x^2 + x + 2 /.x->2
(* Use lists of rules to replace many variables *)
x^2+2y-5/.{x->1,y->Log[42]}
(* Change a name of a variable by using a rule *)
Sin[x]+x/.x->ξ

Solving Equations

Basic Equation Solving

The Solve[ ] function can be used to solve equations and systems of equations, with or without side constraints. The basic usage of Solve is Solve[ eqn, variable ]. Equations are denoted with ==

(* Solve a simple, linear equation for x *)
Solve[x+5==3,x]
(* If there is only one variable, the second parameter can be omitted *)
Solve[x+5==3]

The Solve[ ] function returns a list of list of the following form:
result = { solution1, solution2, ... }
where every solution is a again a list of rules for every variable

(* Solving an equation with multiple solutions *)
Solve[x^3-2x^2-x==-2,x]

To solve systems of equations (or inequalities), use a list of equations in the first argument.

(* Solve a system of two linear equations for two varibles *)
Solve[{x+y==1,x-y==3},{x,y}]
(* Solve an equation with a side constraint *)
Solve[{x^3-2x^2-x==-2,x≥0},x]

Using the results of Solve

The result of solve are lists of lists of rules that can be used to plug in the result values in new equations.

(* To extract the solution values, apply the returned rules *)
solution=Solve[x^3-2x^2-x==-2,x];
x/.solution
(* If just one sultion is needed, use, e.g., First *)
solution=First[Solve[x^3-2x^2-x==-2,x]];
x/.solution
(* The results can be plugged in directly into other equations *)
solution = First[Solve[{x+y==1,x-y==3},{x,y}]];
42*(x+y)/.solution

If a system has no solution, an empty list is returned. If a system has infinitely many solution, all of them are returned (if not all variables are determined by, a warning is stated)

(* A system with no solution *)
Solve[{(x+3)^2==2,x≥0},x]
(* A system with infitely many solutions *)
Solve[Sin[x]==0]
(* A system with redundant equalities *)
Solve[{x+y==1,2x+2y==2},{x,y}]

Advanced solve options

The third argument of Solve can be used to specify a domain (which can be Reals, Integers or Complexes)

Solve[x^3-x^2+x==1,x]
Solve[x^3-x^2+x==1,x,Reals]
Solve[x^2==a,x,Reals]

If an equation has many solutions, but only one instance is needed, the function FindInstance may be helpful

(* A system with many solutions *)
Solve[x^2-3y^2==1&&10<x<100,{x,y}]
(* Returns some integer solution of the system above *)
FindInstance[x^2-3y^2==1&&10<x<100,{x,y},Integers]

Solving systems of linear equations

The function LinearSolve[ A, b ] provides an easy way to solve systems of linear equations of the form A x = b.

(* Setup a linear system of the form A x = b *)
A=({{1, 0, 1}, {2, 1, 2}, {3, 1, -1}});
b={43,85,-40};
(* Solve the Linear System *)
x=LinearSolve[A,b]
(* Verify that x is indeed a solution *)
A.x==b

Reducing Systems of (In-)equalities

The function Reduce[ ] reduces systems by solving equalities and inequalities as far as possible and simplifying terms.

(* Reduce a system *)
Reduce[{x^2+y^2==1,x≥0},{x,y},Reals]
Reduce[{x^2+1≥0},x,Reals]
Reduce[{x^2≥a},x]

Simplify and Conditional Expressions

Some expressions (e.g. solutions obtained from Solve) may be not in the simplest form. To simplify, use the functions Simplify or FullSimplify (see documentation for differences).

Simplify[((-252-462 x-126 x^2+126 x^3+42 x^4)/(21+42 x+21 (x^2)))]

Some Expressions can be simplified using assumptions. Use the Assumption option and assign a (list of) assumptions to it

(* An expression that can not be simplified without further assumptions *)
Simplify[ 1 + Abs[x-1]]
(* Using assumptions the expression can be simplified *)
Simplify[ 1 + Abs[x-1],Assumptions->{x≥1}]
Simplify[x^2==-1,Assumptions->{x∈Reals}]

Results of Solve may be Conditional Expressions if the solution depends on a parameter. Conditional Expressions can be removed by using Simplify with Assumptions.

solution=Solve[x^2==a,x,Reals]
Simplify[solution,Assumptions->a>0]